﻿using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;


namespace AXTesting
{
    /// <summary>
    /// Summary description for Item1Test
    /// </summary>
    [CodedUITest]
    public class CreateASalesOrder
    {
        public CreateASalesOrder()
        {
        }

        [TestMethod]
        public void CodedUITestMethod1()

        {
            // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
            // Find Dynamics AX Window
            WinWindow DynamicsAXWindow = new WinWindow();
            DynamicsAXWindow.TechnologyName = "MSAA";
            DynamicsAXWindow.SearchProperties.Add("Name", "Microsoft Dynamics AX", PropertyExpressionOperator.Contains);
            DynamicsAXWindow.SearchProperties.Add("ClassName", "AxMainFrame");

            // Click selector
            WinSplitButton selectorSplitButton = new WinSplitButton(DynamicsAXWindow);
            selectorSplitButton.TechnologyName = "MSAA";
            selectorSplitButton.SearchProperties.Add("Name", "");
            selectorSplitButton.SearchProperties.Add("ControlType", "SplitButton");
            Mouse.Click(selectorSplitButton);

            // Find the DropDown Window
            WinWindow menuDropDownWindow = new WinWindow();
            menuDropDownWindow.TechnologyName = "MSAA";
            menuDropDownWindow.SearchProperties.Add("AccessibleName", "DropDown");
            menuDropDownWindow.SearchProperties.Add("ControlType", "Window");

            // Click CEU item
            WinMenuItem cEUMenuItem = new WinMenuItem(menuDropDownWindow);
            cEUMenuItem.TechnologyName = "MSAA";
            cEUMenuItem.SearchProperties.Add("Name", "Contoso Entertainment Systems", PropertyExpressionOperator.Contains);
            cEUMenuItem.SearchProperties.Add("Name", "(West)", PropertyExpressionOperator.Contains);
            cEUMenuItem.SearchProperties.Add("ControlType", "MenuItem");
            Mouse.Click(cEUMenuItem);

            // Click CEU split button
            WinSplitButton cEUSplitButton = new WinSplitButton(DynamicsAXWindow);
            cEUSplitButton.TechnologyName = "MSAA";
            cEUSplitButton.SearchProperties.Add("Name", "CEU");
            cEUSplitButton.SearchProperties.Add("ControlType", "SplitButton");
            Mouse.Click(cEUSplitButton, new Point(cEUSplitButton.BoundingRectangle.Width - 3,cEUSplitButton.BoundingRectangle.Height / 2 ));

            // Click Sales and Marketingitem
            WinMenuItem salesAndMarketingMenuItem = new WinMenuItem(menuDropDownWindow);
            salesAndMarketingMenuItem.TechnologyName = "MSAA";
            salesAndMarketingMenuItem.SearchProperties.Add("Name", "Sales and marketing");
            salesAndMarketingMenuItem.SearchProperties.Add("ControlType", "MenuItem");
            Mouse.Click(salesAndMarketingMenuItem);

            //click Sales and Marketing split button
            WinSplitButton salesAndMarketingSplitButton = new WinSplitButton(DynamicsAXWindow);
            salesAndMarketingSplitButton.TechnologyName = "MSAA";
            salesAndMarketingSplitButton.SearchProperties.Add("Name", "Sales and marketing");
            salesAndMarketingSplitButton.SearchProperties.Add("ControlType", "SplitButton");
            Mouse.Click(salesAndMarketingSplitButton, new Point(salesAndMarketingSplitButton.BoundingRectangle.Width - 3, salesAndMarketingSplitButton.BoundingRectangle.Height / 2));
            
            //click Common item
            menuDropDownWindow.Find();
            WinMenuItem commonMenuItem = new WinMenuItem(menuDropDownWindow);
            commonMenuItem.TechnologyName = "MSAA";
            commonMenuItem.SearchProperties.Add("Name", "Common");
            commonMenuItem.SearchProperties.Add("ControlType", "MenuItem");
            commonMenuItem.Find();
            Mouse.Click(commonMenuItem);
            
            //click Common split button
            WinSplitButton commonSplitButton = new WinSplitButton(DynamicsAXWindow);
            commonSplitButton.TechnologyName = "MSAA";
            commonSplitButton.SearchProperties.Add("Name", "Common");
            commonSplitButton.SearchProperties.Add("ControlType", "SplitButton");
            commonSplitButton.Find();
            Mouse.Click(commonSplitButton, new Point(commonSplitButton.BoundingRectangle.Width - 3, commonSplitButton.BoundingRectangle.Height / 2));
            
            //click Sales orders item
            menuDropDownWindow.Find();
            WinMenuItem salesOrdersMenuItem = new WinMenuItem(menuDropDownWindow);
            salesOrdersMenuItem.TechnologyName = "MSAA";
            salesOrdersMenuItem.SearchProperties.Add("Name", "Sales orders");
            salesOrdersMenuItem.SearchProperties.Add("ControlType", "MenuItem");
            salesOrdersMenuItem.Find();
            Mouse.Click(salesOrdersMenuItem);
            
            //click sales orders split button
            WinSplitButton salesOrdersSplitButton = new WinSplitButton(DynamicsAXWindow);
            salesOrdersSplitButton.TechnologyName = "MSAA";
            salesOrdersSplitButton.SearchProperties.Add("Name", "Sales orders");
            salesOrdersSplitButton.SearchProperties.Add("ControlType", "SplitButton");
            salesOrdersSplitButton.Find();
            Mouse.Click(salesOrdersSplitButton, new Point(salesOrdersSplitButton.BoundingRectangle.Width - 3, salesOrdersSplitButton.BoundingRectangle.Height / 2));
            
            //click allsalesorders item
            menuDropDownWindow.Find();
            WinMenuItem allSalesOrdersMenuItem = new WinMenuItem(menuDropDownWindow);
            allSalesOrdersMenuItem.TechnologyName = "MSAA";
            allSalesOrdersMenuItem.SearchProperties.Add("Name", "All sales orders");
            allSalesOrdersMenuItem.SearchProperties.Add("ControlType", "MenuItem");
            allSalesOrdersMenuItem.Find();
            Mouse.Click(allSalesOrdersMenuItem);
            
            //click New Sales order
            DynamicsAXWindow.Find();
            WinMenuItem newSalesOrdersMenuItem = new WinMenuItem(DynamicsAXWindow);
            newSalesOrdersMenuItem.TechnologyName = "MSAA";
            newSalesOrdersMenuItem.SearchProperties.Add("Name", "Sales order");
            newSalesOrdersMenuItem.SearchProperties.Add("ControlType", "MenuItem");
            newSalesOrdersMenuItem.Find();
            Mouse.Click(newSalesOrdersMenuItem);
            
            // Find Create sales order window
            WinWindow createSalesOrderWindow = new WinWindow();
            createSalesOrderWindow.TechnologyName = "MSAA";
            createSalesOrderWindow.SearchProperties.Add("Name", "Create sales order", PropertyExpressionOperator.Contains);
            createSalesOrderWindow.SearchProperties.Add("ClassName", "AxTopLevelFrame");
            createSalesOrderWindow.SearchProperties.Add("ControlType", "Window");
            createSalesOrderWindow.Find();

            //Select Customer = 1101
            WinEdit customerAccountEdit = new WinEdit(createSalesOrderWindow);
            customerAccountEdit.TechnologyName = "MSAA";
            customerAccountEdit.SearchProperties.Add("Name", "Customer account");
            customerAccountEdit.SearchProperties.Add("ControlType", "Edit");
            customerAccountEdit.Find();
            customerAccountEdit.SetProperty("Text", "1101");
            Keyboard.SendKeys("{TAB}");
            
            //click ok
            WinButton createSalesOrderOKButton = new WinButton(createSalesOrderWindow);
            createSalesOrderOKButton.TechnologyName = "MSAA";
            createSalesOrderOKButton.SearchProperties.Add("Name", "OK");
            createSalesOrderOKButton.SearchProperties.Add("ControlType", "Button");
            createSalesOrderOKButton.Find();
            Mouse.Click(createSalesOrderOKButton);
            
            // Find Sales Order Window
            WinWindow salesOrderWindow = new WinWindow();
            salesOrderWindow.TechnologyName = "MSAA";
            salesOrderWindow.SearchProperties.Add("Name", "Sales order", PropertyExpressionOperator.Contains);
            salesOrderWindow.SearchProperties.Add("ClassName", "AxTopLevelFrame");
            salesOrderWindow.SearchProperties.Add("ControlType", "Window");
            salesOrderWindow.Find();

            WinCell typeRow1 = new WinCell(salesOrderWindow);
            typeRow1.TechnologyName = "MSAA";
            typeRow1.SearchProperties.Add("Name", "Type Row 1");
            typeRow1.SearchProperties.Add("ControlType", "Cell");
            typeRow1.Find();
            Mouse.Click(typeRow1);
            
            //enter item = 1001
            WinCell itemNumberRow1 = new WinCell(salesOrderWindow);
            itemNumberRow1.TechnologyName = "MSAA";
            itemNumberRow1.SearchProperties.Add("Name", "Item number Row 1");
            itemNumberRow1.SearchProperties.Add("ControlType", "Cell");
            itemNumberRow1.Find();
            Mouse.Click(itemNumberRow1);

            WinEdit itemNumberEdit = new WinEdit(salesOrderWindow);
            itemNumberEdit.TechnologyName = "MSAA";
            itemNumberEdit.SearchProperties.Add("Name", "Item number");
            itemNumberEdit.SearchProperties.Add("ControlType", "Edit");
            itemNumberEdit.Find();
            itemNumberEdit.SetProperty("Text", "1001");

            //enter quantity = 1
            WinCell quantityRow1 = new WinCell(salesOrderWindow);
            quantityRow1.TechnologyName = "MSAA";
            quantityRow1.SearchProperties.Add("Name", "Quantity Row 1");
            quantityRow1.SearchProperties.Add("ControlType", "Cell");
            quantityRow1.Find();
            Mouse.Click(quantityRow1);

            WinEdit quantityEdit = new WinEdit(salesOrderWindow);
            quantityEdit.TechnologyName = "MSAA";
            quantityEdit.SearchProperties.Add("Name", "Quantity");
            quantityEdit.SearchProperties.Add("ControlType", "Edit");
            quantityEdit.Find();
            quantityEdit.SetProperty("Text", "1");

            //enter configuration = HD
            WinCell configurationRow1 = new WinCell(salesOrderWindow);
            configurationRow1.TechnologyName = "MSAA";
            configurationRow1.SearchProperties.Add("Name", "Configuration Row 1");
            configurationRow1.SearchProperties.Add("ControlType", "Cell");
            configurationRow1.Find();
            Mouse.Click(configurationRow1);

            WinEdit configurationEdit = new WinEdit(salesOrderWindow);
            configurationEdit.TechnologyName = "MSAA";
            configurationEdit.SearchProperties.Add("Name", "Configuration");
            configurationEdit.SearchProperties.Add("ControlType", "Edit");
            configurationEdit.Find();
            configurationEdit.SetProperty("Text", "HD");
            
            //enter size = 42
            WinCell sizeRow1 = new WinCell(salesOrderWindow);
            sizeRow1.TechnologyName = "MSAA";
            sizeRow1.SearchProperties.Add("Name", "Size Row 1");
            sizeRow1.SearchProperties.Add("ControlType", "Cell");
            sizeRow1.Find();
            Mouse.Click(sizeRow1);

            WinEdit sizeEdit = new WinEdit(salesOrderWindow);
            sizeEdit.TechnologyName = "MSAA";
            sizeEdit.SearchProperties.Add("Name", "Size");
            sizeEdit.SearchProperties.Add("ControlType", "Edit");
            sizeEdit.Find();
            sizeEdit.SetProperty("Text", "42");
            sizeEdit.SetProperty("Text", "42");

            //enter color = 01
            WinCell colorRow1 = new WinCell(salesOrderWindow);
            colorRow1.TechnologyName = "MSAA";
            colorRow1.SearchProperties.Add("Name", "Color Row 1");
            colorRow1.SearchProperties.Add("ControlType", "Cell");
            colorRow1.Find();
            Mouse.Click(colorRow1);

            WinEdit colorEdit = new WinEdit(salesOrderWindow);
            colorEdit.TechnologyName = "MSAA";
            colorEdit.SearchProperties.Add("Name", "Color");
            colorEdit.SearchProperties.Add("ControlType", "Edit");
            colorEdit.Find();
            colorEdit.SetProperty("Text", "01");

            //define Line Details Fast tab
            WinClient lineDetailsClient = new WinClient(salesOrderWindow);
            lineDetailsClient.TechnologyName = "MSAA";
            lineDetailsClient.SearchProperties.Add("Name", "Line details");
            lineDetailsClient.SearchProperties.Add("ControlType", "Client");
            lineDetailsClient.Find();
            Mouse.Click(lineDetailsClient);

            //click Setup tab
            WinClient setupTabPage = new WinClient(salesOrderWindow);
            setupTabPage.TechnologyName = "UIA";
            setupTabPage.SearchProperties.Add("Name", "Setup");
            setupTabPage.SearchProperties.Add("ControlType", "TabPage");
            setupTabPage.Find();
            Mouse.Click(setupTabPage);
            
            //enter sales tax group = NDFAR
            WinEdit salesTaxGroupEdit = new WinEdit(salesOrderWindow);
            salesTaxGroupEdit.TechnologyName = "MSAA";
            salesTaxGroupEdit.SearchProperties.Add("Name", "Sales tax group");
            salesTaxGroupEdit.SearchProperties.Add("ControlType", "Edit");
            salesTaxGroupEdit.Find();
            salesTaxGroupEdit.SetProperty("Text", "NDFAR");
            
            //Enter items sales tax group = ALL
            WinEdit itemSalesTaxGroupEdit = new WinEdit(salesOrderWindow);
            itemSalesTaxGroupEdit.TechnologyName = "MSAA";
            itemSalesTaxGroupEdit.SearchProperties.Add("Name", "Item sales tax group");
            itemSalesTaxGroupEdit.SearchProperties.Add("ControlType", "Edit");
            itemSalesTaxGroupEdit.Find();
            itemSalesTaxGroupEdit.SetProperty("Text", "ALL");
            
            //click to close the Line Details fasttab
            Mouse.Click(lineDetailsClient);
            
            //click add line in the Lines fast tab
            WinMenuItem addLineMenuItem = new WinMenuItem(salesOrderWindow);
            addLineMenuItem.TechnologyName = "MSAA";
            addLineMenuItem.SearchProperties.Add("Name", "Add line");
            addLineMenuItem.SearchProperties.Add("ControlType", "MenuItem");
            addLineMenuItem.Find();
            Mouse.Click(addLineMenuItem);

            WinCell typeRow2 = new WinCell(salesOrderWindow);
            typeRow2.TechnologyName = "MSAA";
            typeRow2.SearchProperties.Add("Name", "Type Row 2");
            typeRow2.SearchProperties.Add("ControlType", "Cell");
            typeRow2.Find();
            Mouse.Click(typeRow2);
            
            //enter item = 1003
            WinCell itemNumberRow2 = new WinCell(salesOrderWindow);
            itemNumberRow2.TechnologyName = "MSAA";
            itemNumberRow2.SearchProperties.Add("Name", "Item number Row 2");
            itemNumberRow2.SearchProperties.Add("ControlType", "Cell");
            itemNumberRow2.Find();
            Mouse.Click(itemNumberRow2);

            itemNumberEdit.Find();
            itemNumberEdit.SetProperty("Text", "1003");
            
            //enter quantity = 1
            WinCell quantityRow2 = new WinCell(salesOrderWindow);
            quantityRow2.TechnologyName = "MSAA";
            quantityRow2.SearchProperties.Add("Name", "Quantity Row 2");
            quantityRow2.SearchProperties.Add("ControlType", "Cell");
            quantityRow2.Find();
            Mouse.Click(quantityRow2);

            quantityEdit.Find();
            quantityEdit.SetProperty("Text", "1");
            
            //click Line Details fast tab
            //click Setup tab
            Mouse.Click(lineDetailsClient);
            Mouse.Click(setupTabPage);
            
            //enter sales tax group = CA
            salesTaxGroupEdit.Find();
            salesTaxGroupEdit.SetProperty("Text", "CA");
            
            //Enter items sales tax group = ALL
            itemSalesTaxGroupEdit.Find();
            itemSalesTaxGroupEdit.SetProperty("Text", "ALL");
            
            //Close the Line Details fasttab
            Mouse.Click(lineDetailsClient);
            
            //Select Sell
            WinButton sellTabButton = new WinButton(salesOrderWindow);
            sellTabButton.TechnologyName = "MSAA";
            sellTabButton.SearchProperties.Add("Name", "SellTab");
            sellTabButton.SearchProperties.Add("ControlType", "Button");
            sellTabButton.Find();
            Mouse.Click(sellTabButton);

            //Select Charges
            WinMenuItem chargesMenuItem = new WinMenuItem(salesOrderWindow);
            chargesMenuItem.TechnologyName = "MSAA";
            chargesMenuItem.SearchProperties.Add("Name", "Charges");
            chargesMenuItem.SearchProperties.Add("ControlType", "MenuItem");
            chargesMenuItem.Find();
            Mouse.Click(chargesMenuItem);

            // Find Charges transactions window
            WinWindow chargesTransactionsWindow = new WinWindow();
            chargesTransactionsWindow.TechnologyName = "MSAA";
            chargesTransactionsWindow.SearchProperties.Add("Name", "Charges transactions", PropertyExpressionOperator.Contains);
            chargesTransactionsWindow.SearchProperties.Add("ControlType", "Window");
            chargesTransactionsWindow.Find();
            
            //Enter Misc Charge Code = 01
            WinEdit chargesCodeEdit = new WinEdit(chargesTransactionsWindow);
            chargesCodeEdit.TechnologyName = "MSAA";
            chargesCodeEdit.SearchProperties.Add("Name", "Charges code");
            chargesCodeEdit.SearchProperties.Add("ControlType", "Edit");
            chargesCodeEdit.Find();
            chargesCodeEdit.SetProperty("Text", "01");
            
            //Enter Value = 25.00
            WinEdit chargesValueEdit = new WinEdit(chargesTransactionsWindow);
            chargesValueEdit.TechnologyName = "MSAA";
            chargesValueEdit.SearchProperties.Add("Name", "Charges value");
            chargesValueEdit.SearchProperties.Add("ControlType", "Edit");
            chargesValueEdit.Find();
            chargesValueEdit.SetProperty("Text", "25.00");
            
            //Close window
            WinButton chargesTransactionsWindowCloseButton = new WinButton(chargesTransactionsWindow);
            chargesTransactionsWindowCloseButton.TechnologyName = "UIA";
            chargesTransactionsWindowCloseButton.SearchProperties.Add("Name", "Close");
            chargesTransactionsWindowCloseButton.SearchProperties.Add("ControlType", "Button");
            chargesTransactionsWindowCloseButton.Find();
            Mouse.Click(chargesTransactionsWindowCloseButton);
            
            //Select Sales order
            WinButton salesOrderTabButton = new WinButton(salesOrderWindow);
            salesOrderTabButton.TechnologyName = "MSAA";
            salesOrderTabButton.SearchProperties.Add("Name", "SalesOrderTab");
            salesOrderTabButton.SearchProperties.Add("ControlType", "Button");
            salesOrderTabButton.Find();
            Mouse.Click(salesOrderTabButton);

            //Header view

            WinCheckBox headerViewCheckBox = new WinCheckBox(salesOrderWindow);
            headerViewCheckBox.TechnologyName = "MSAA";
            headerViewCheckBox.SearchProperties.Add("Name", "Header view");
            headerViewCheckBox.SearchProperties.Add("ControlType", "CheckBox");
            headerViewCheckBox.Find();
            Mouse.Click(headerViewCheckBox);
            
            //Open the Financial dimensions fast tab
            WinClient financialDimensionsClient = new WinClient(salesOrderWindow);
            financialDimensionsClient.TechnologyName = "MSAA";
            financialDimensionsClient.SearchProperties.Add("Name", "Financial dimensions");
            financialDimensionsClient.SearchProperties.Add("ControlType", "Client");
            financialDimensionsClient.Find();
            Mouse.Click(financialDimensionsClient);

            //Enter cost center = OU 4571(Contoso USA)
            WinEdit costCenterEdit = new WinEdit(salesOrderWindow);
            costCenterEdit.TechnologyName = "MSAA";
            costCenterEdit.SearchProperties.Add("Name", "CostCenter");
            costCenterEdit.SearchProperties.Add("ControlType", "Edit");
            costCenterEdit.SetProperty("Text", "OU_4571");
            
            Mouse.Click(financialDimensionsClient);

            WinButton salesOrderWindowCloseButton = new WinButton(salesOrderWindow);
            salesOrderWindowCloseButton.TechnologyName = "UIA";
            salesOrderWindowCloseButton.SearchProperties.Add("Name", "Close");
            salesOrderWindowCloseButton.SearchProperties.Add("ControlType", "Button");
            salesOrderWindowCloseButton.Find();
            Mouse.Click(salesOrderWindowCloseButton);

            WinWindow popupWindow = new WinWindow();
            popupWindow.TechnologyName = "MSAA";
            popupWindow.SearchProperties.Add("ClassName", "AxPopupFrame");
            popupWindow.SearchProperties.Add("ControlType", "Window");
            popupWindow.Find();

            WinCheckBox updateDimensionCheckBox = new WinCheckBox(popupWindow);
            updateDimensionCheckBox.TechnologyName = "MSAA";
            updateDimensionCheckBox.SearchProperties.Add("Name", "Update Dimension");
            updateDimensionCheckBox.SearchProperties.Add("ControlType", "CheckBox");
            updateDimensionCheckBox.Find();

            WinButton popupWindowOKButton = new WinButton(popupWindow);
            popupWindowOKButton.TechnologyName = "MSAA";
            popupWindowOKButton.SearchProperties.Add("Name", "OK");
            popupWindowOKButton.SearchProperties.Add("ControlType", "Button");
            popupWindowOKButton.Find();

            if (updateDimensionCheckBox.Checked)
            {
                Mouse.Click(popupWindowOKButton);
            }
            else
            {
                Mouse.Click(updateDimensionCheckBox);
                Mouse.Click(popupWindowOKButton);
            }
            

            //Select Pick and Pack
            //Select Packing Slip Journals
            //Expected results
            //Select Invoice
            //Select Generate
            //Select Invoice
            //Change quantity parameter = All
            //Select Sales Tax
            //Change the actual sales tax to 305.00
            //Click Adjustments tab
            //Click Apply button
            //Close the window
            //Click Ok
            //Click OK from warning
            //Select Invoice
            //Select Journals
            //Select Invoice
            //Expected results
            //Click Lines tab
            //Expected results
            //Click back on the Overview tab
            //Click Posted sales tax button
            //Expected results
            //Close all forms


            // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
            
        }

        #region Additional test attributes

        // You can use the following additional attributes as you write your tests:

        ////Use TestInitialize to run code before running each test 
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{        
        //    // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
        //    // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
        //}

        ////Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{        
        //    // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
        //    // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
        //}

        #endregion

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        private TestContext testContextInstance;

    }
}
